<template>
  <div class="login">
    <image style="width: 100vw;height: 100vh;position: fixed;left: 0;top: 0;z-index: -1" src="/static/image/login_bg.png"></image>
    <Navbar isBack></Navbar>
    <div class="login-form">
      <image style="width: 240rpx;height: 240rpx" src="/static/image/logo.png"></image>
      <div class="login-button">
        <button class="primary-button" open-type="getPhoneNumber" @getphonenumber="onGetPhoneNumber">授权一键登录</button>
      </div>
    </div>
    <view class="login-footer">
      <view class="check" style="display: flex;align-items: center">
        <div style="display: flex;align-items: center" @click="setAgree()">
          <image v-if="agree" class="publish-icon" src="/static/radio_fill.png"></image>
          <image v-else class="publish-icon" src="/static/radio.png"></image>
          <text style="color: #8F959E;font-size: 28rpx">我已阅读并同意</text>
        </div>
        <text style="color: #E68627"  @click="setAgree2('privacy_policy')">《隐私政策》</text>
        <text style="color: #8F959E;font-size: 28rpx">和</text>
        <text style="color: #E68627"  @click="setAgree2('user_agreement')">《用户协议》</text>
      </view>
    </view>
  </div>
</template>
<script>
import Page from "../../components/Page.vue";
import Navbar from "../../components/Navbar.vue";

export default {
  name: "index",
  components: {Navbar, Page},
  data() {
    return {
      agree: false
    }
  },
  onLoad() {
    this.$store.commit('LOGOUT')
  },
  methods: {
    onGetPhoneNumber(e) {
      if (!this.agree) {
        uni.showToast({
          title: '请阅读并同意《登录协议》',
          icon: 'none'
        })
        return
      }
      console.log(e)
      if(e.detail.errMsg === 'getPhoneNumber:ok') {
        uni.showLoading({
          title: '登录中...'
        })
        let that = this
        const { detail: { encryptedData, iv } } = e;
        wx.login({
          async success(loginRes) {
            if (loginRes.code) {
              try {
                // 发送code、encryptedData、iv到后端
                const response = await that.$api.wxLogin({
                  code: loginRes.code,
                  encryptedData,
                  iv,
                });
                that.$store.commit('SET_USERINFO', {
                  ...response,
                  is_register: false
                })
                // uni.reLaunch({
                //   url: '/pages/getColor/getColor'
                // })
                // back
                setTimeout(() => {
                  // 提交逻辑
                  uni.showToast({ title: '登录成功', icon: 'success' });
                },100)
                if (response.is_register) {
                  // 重定向完善个人信息
                  uni.redirectTo({
                    url: '/pages/mine/edit'
                  })
                } else {
                  uni.navigateBack()
                }
              } catch (error) {
                uni.showToast({
                  title: error,
                  icon: 'none'
                })
                console.error('请求后端接口失败:', error);
              }
            }
          }
        })
      } else {
        uni.showToast({
          title: e.detail.errMsg,
          icon: 'none'
        })
      }
    },
    setAgree() {
      this.agree = !this.agree;
    },
    setAgree2(key) {
      uni.navigateTo({
        url: '/pages/login/service?key=' + key
      })
    },
  }

}
</script>

<style lang="scss">
.login {
  height: 100%;
  overflow: hidden;
  .login-form {
    display: flex;
    flex-direction: column;
    align-items: center;
    margin-top: 200rpx;
  }
  .login-button {
    padding: 60rpx 0 30rpx 0;
    display: flex;
    align-items: center;
    justify-content: center;

    .primary-button {
      width: 630rpx;
      background: rgba(105, 192, 110, 1);
    }
  }

  .login-footer {
    padding-bottom: 100rpx;
    width: 100%;
    font-size: 28rpx;
    font-weight: normal;
    color: #999;
    display: flex;
    justify-content: center;
    margin-top: 30rpx;

    .publish-icon {
      width: 32rpx;
      height: 32rpx;
      margin-right: 10rpx;
    }
  }
}
</style>
